
package com.epfresh.supplier.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.AgentMyApplyRecord;

import java.text.SimpleDateFormat;
import java.util.Date;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class AgentMyApplyViewBinder
        extends ItemViewBinder<AgentMyApplyRecord, AgentMyApplyViewBinder.ViewHolder> implements View.OnClickListener {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_agent_my_apply, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull AgentMyApplyRecord post) {
        if (post.getAgentState() == 1) {
            holder.tv_agent_apply_again.setVisibility(View.GONE);
            holder.tv_agent_apply_state.setText("申请中");
        } else if (post.getAgentState() == 6) {
            holder.tv_agent_apply_again.setVisibility(View.VISIBLE);
            holder.tv_agent_apply_state.setText("已解除");
        } else if (post.getAgentState() == 3){
            holder.tv_agent_apply_again.setVisibility(View.VISIBLE);
            holder.tv_agent_apply_state.setText("申请被拒");
        }
        if(post.getCreatedTime()!=null&&post.getCreatedTime().length()>10)
//            post.setCreatedTime(post.getCreatedTime().substring(0,10));
        holder.tv_agent_apply_time.setText(formatDate(post.getCreatedTime()));
        holder.tv_agent_apply_base.setText(post.getName());
        holder.tv_agent_apply_again.setTag(R.id.item_key_status, post);
        holder.tv_agent_apply_again.setOnClickListener(this);
        holder.tv_agent_apply_go.setTag(R.id.item_key_status, post);
        holder.tv_agent_apply_go.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_status);
        if (onRecyclerViewItemClickListener != null && o instanceof AgentMyApplyRecord) {
            onRecyclerViewItemClickListener.onItemClick(v, 0, o);
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        private View tv_agent_apply_go, tv_agent_apply_again;
        private TextView tv_agent_apply_state, tv_agent_apply_base, tv_agent_apply_time;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_agent_apply_go = itemView.findViewById(R.id.tv_agent_apply_go);
            tv_agent_apply_again = itemView.findViewById(R.id.tv_agent_apply_again);
            tv_agent_apply_state = itemView.findViewById(R.id.tv_agent_record_state);
            tv_agent_apply_base = itemView.findViewById(R.id.tv_agent_apply_base);
            tv_agent_apply_time = itemView.findViewById(R.id.tv_agent_apply_time);
        }
    }

    private String formatDate(String timeStamp) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date(Long.parseLong(timeStamp)));
    }
}
